package jsoundcapturever2;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.util.Vector;

import javax.sound.sampled.AudioFormat;
import javax.swing.JPanel;

public class SamplingGraph extends JPanel implements Runnable {

	private Thread thread;
	private Font font10 = new Font("serif", Font.PLAIN, 10);
	private Font font12 = new Font("serif", Font.PLAIN, 12);
	Color jfcBlue = new Color(204, 204, 255);
	Color pink = new Color(255, 175, 175);
	AudioFormat format;
	float[] audioData;
	Vector lines = new Vector();
	double seconds, duration;

	public SamplingGraph() {
		setBackground(new Color(20, 20, 20));
	}

	public void createWaveForm(AudioFormat format, float[] audioData, double duration) {
		this.format = format;
		this.audioData = audioData;
		this.duration = duration;
		Dimension d = getSize();
		int w = d.width;
		int h = d.height - 15;
		lines.removeAllElements();
//		int frames_per_pixel = audioBytes.length / format.getFrameSize() / w;
		int frames_per_pixel = audioData.length / format.getFrameSize() / w;
		byte my_byte = 0;
		double y_last = 0;
		int numChannels = format.getChannels();
		for (double x = 0; x < w && audioData != null; x++) {
			int idx = (int) (frames_per_pixel * numChannels * x);
			if (format.getSampleSizeInBits() == 8) {
				my_byte = (byte) audioData[idx];
			} else {
				my_byte = (byte) (128 * audioData[idx] / 32768);
			}
			double y_new = (double) (h * (128 - my_byte) / 256);
			lines.add(new Line2D.Double(x, y_last, x, y_new));
			y_last = y_new;
		}
		repaint();
	}

	public void paint(Graphics g) {

		Dimension d = getSize();
		int w = d.width;
		int h = d.height - 15;
		int INFOPAD = 15;

		Graphics2D g2 = (Graphics2D) g;
		g2.setBackground(getBackground());
		g2.clearRect(0, 0, w, h);
		g2.setColor(Color.white);
		g2.fillRect(0, h - INFOPAD, w, INFOPAD);

		g2.setColor(Color.black);
		g2.setFont(font12);

		if (lines != null) {
			// .. render sampling graph ..
			g2.setColor(jfcBlue);
			for (int i = 1; i < lines.size(); i++) {
				System.out.println("Drawing..");
				g2.draw((Line2D) lines.get(i));
			}

			//FIXME: variable seconds should be sync during with playback thread
			// .. draw current position ..
			if (seconds != 0) {
				double loc = seconds / duration * w;
				g2.setColor(pink);
				g2.setStroke(new BasicStroke(3));
				g2.draw(new Line2D.Double(loc, 0, loc, h - INFOPAD - 2));
			}
		}

	}

	public void start() {
		thread = new Thread(this);
		thread.setName("SamplingGraph");
		thread.start();
		// seconds = 0;
	}

	public void stop() {
		if (thread != null) {
			thread.interrupt();
		}
		thread = null;
	}

	public void run() {
		// seconds = 0;
		// while (thread != null) {
		// if ((playback.line != null) && (playback.line.isOpen()) ) {
		//
		// long milliseconds = (long)(playback.line.getMicrosecondPosition() /
		// 1000);
		// seconds = milliseconds / 1000.0;
		// } else if ( (capture.line != null) && (capture.line.isActive()) ) {
		//
		// long milliseconds = (long)(capture.line.getMicrosecondPosition() /
		// 1000);
		// seconds = milliseconds / 1000.0;
		// }
		//
		// try { thread.sleep(100); } catch (Exception e) { break; }

		// repaint();

		// while ((capture.line != null && !capture.line.isActive()) ||
		// (playback.line != null && !playback.line.isOpen()))
		// {
		// try { thread.sleep(10); } catch (Exception e) { break; }
		// }
		// }
		// seconds = 0;
		repaint();
	}

}
